package com.dimples.controller.system;

import com.dimples.common.annotation.OpsLog;
import com.dimples.common.enums.OpsLogTypeEnum;
import com.dimples.common.service.DimplesJwtTokenServiceImpl;
import com.dimples.common.transport.Result;
import com.dimples.common.util.HttpContextUtil;
import com.dimples.controller.view.BaseWebController;
import com.dimples.system.entity.SysNotice;
import com.dimples.system.service.SysNoticeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/6/26
 */
@Slf4j
@Api(tags = "消息管理")
@RestController
@RequestMapping(value = BaseWebController.API_SYSTEM_PREFIX + "notice")
@RequiredArgsConstructor
public class SysNoticeController {

    private final DimplesJwtTokenServiceImpl tokenService;
    private final SysNoticeService sysNoticeService;

    @ApiOperation(value = "查询消息")
    @ApiOperationSupport(order = 1)
    @OpsLog(value = "查询消息", type = OpsLogTypeEnum.SELECT)
    @GetMapping
    public Result<List<Map<String, Object>>> selectSysNoticeList() {
        List<Map<String, Object>> result = CollUtil.newArrayList();
        SysNotice publicParam = new SysNotice();
        publicParam.setType("public");

        SysNotice privateParam = new SysNotice();
        privateParam.setType("private");
        privateParam.setAccept(tokenService.getLoginUserFromToken(HttpContextUtil.getRequest()).getUserId());

        SysNotice noticeParam = new SysNotice();
        noticeParam.setType("notice");

        Map<String, Object> publicArray = CollUtil.newHashMap();
        publicArray.put("id", "1");
        publicArray.put("title", "公告");
        publicArray.put("children", sysNoticeService.selectSysNoticeList(publicParam));

        Map<String, Object> privateArray = CollUtil.newHashMap();
        privateArray.put("id", "2");
        privateArray.put("title", "私信");
        privateArray.put("children", sysNoticeService.selectSysNoticeList(privateParam));

        Map<String, Object> noticeArray = CollUtil.newHashMap();
        noticeArray.put("id", "3");
        noticeArray.put("title", "通知");
        noticeArray.put("children", sysNoticeService.selectSysNoticeList(noticeParam));

        result.add(publicArray);
        result.add(privateArray);
        result.add(noticeArray);
        return Result.success(result);
    }
}
